﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PEC
{
    public partial class FrmMain : Form
    {
        PecController _pecController;
        public FrmTerminator Terminator { get; set; }
        public FrmMain(PecController pecController)
        {
            _pecController = pecController;
            InitializeComponent();

            TextBox tb = this.toolStripTextBox1.Control as TextBox;
            tb.PasswordChar = '*';
            // Terminator.FormClosed += Terminator_FormClosed;
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

        }

        void Terminator_FormClosed(object sender, FormClosedEventArgs e)
        {
            miConnectDB.Enabled = true;
            miReference.Enabled = true;
            miTerminate.Enabled = true;
        }

        private void terminatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Terminator == null || Terminator.IsDisposed)
            {
                Terminator = new FrmTerminator(_pecController);
                Terminator.FormClosed += Terminator_FormClosed;
            }
            Terminator.Show();
            miConnectDB.Enabled = false;
            miReference.Enabled = false;
            miTerminate.Enabled = false;
        }

        private void connectDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Data.ConnectionFactory.ConnectionString = _pecController.ConnectString;
            Data.ConnectionForm frm = new Data.ConnectionForm();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Util.ConfigTools.Change("ConnectString", Data.ConnectionFactory.ConnectionString);
            }

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void exitAndResetCOMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Util.ConfigTools.Change("ComPorts", "");
            Close();
        }

        private void lineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Forms.FrmChuyen frm = new Forms.FrmChuyen();
            frm.ShowDialog();
        }

        private void rTUOptionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Forms.FrmRtuOption frm = new Forms.FrmRtuOption(_pecController);
            frm.ShowDialog();
        }

        private void timeSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Forms.frmSchedule frm = new Forms.frmSchedule(0, _pecController);
            frm.ShowDialog();
        }

        private void calendarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Forms.FrmOptionTimeOfWeek frm = new Forms.FrmOptionTimeOfWeek();
            frm.ShowDialog();
        }

        private void lenhSXToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Forms.FrmLenhSX frm = new Forms.FrmLenhSX(_pecController);
            frm.ShowDialog();
        }

        private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (toolStripTextBox1.Text == "PTST")
            {
                Forms.FrmConfiguration frm = new Forms.FrmConfiguration();
                frm.ShowDialog();
            }
        }


    }
}
